package com.cloudknow.healthyRun.activity.common;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudknow.activity.BaseActivity;
import com.cloudknow.common.Constant;
import com.cloudknow.healthyRun.R;
import com.cloudknow.utils.ConstomDialog;
import com.cloudknow.utils.PrefsUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Create bu wubin on .
 *
 * @description 启动界面
 */
public class StartFigureActivity extends BaseActivity implements View.OnClickListener {
    private String TAG = getClass().getSimpleName();

    private TextView tv_layout_6;
    private TextView choose_false;
    private TextView choose_true;
    private RelativeLayout layout_index;
    private RelativeLayout layout_second;

    private MyRunnable mRunnable = null;
    private Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    protected int setView() {
        getWindow().setBackgroundDrawable(null);
        // 全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        hideBottomUIMenu();
        return R.layout.activity_common_start_figure;
    }

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeView() {
        initView();
        setXY();
        accordingSwitch();
//        hoDo();

    }

    private void initView() {

        tv_layout_6 = findViewById(R.id.tv_layout_6);
        layout_index = findViewById(R.id.layout_index);
        layout_second = findViewById(R.id.layout_second);

        layout_index.setVisibility(View.VISIBLE);
        layout_second.setVisibility(View.GONE);

        choose_true = findViewById(R.id.choose_true);
        choose_false = findViewById(R.id.choose_false);

        choose_true.setOnClickListener(this);
        choose_false.setOnClickListener(this);

    }

    private void setXY() {
        String textSource = getResources().getString(R.string.home_test_info_79) +
                "<font color='#3E84FE'>" +
                getResources().getString(R.string.home_test_info_80) +
                "</font>" +
                getResources().getString(R.string.home_test_info_81) +

                "<font color='#3E84FE'>" +
                getResources().getString(R.string.home_test_info_82) +
                "</font>" +
                getResources().getString(R.string.home_test_info_83) +
                "<br/>" +
                getResources().getString(R.string.home_test_info_84);
        tv_layout_6.setText(Html.fromHtml(textSource));

    }


    @Override
    protected void loadDataFromServer() {

    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        // 隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    private void accordingSwitch() {
        int isFirstInstall = PrefsUtil.getInt(StartFigureActivity.this, Constant.isFirstInstall);

        switch (isFirstInstall) {
            case 0:
                if (mRunnable == null)
                    mRunnable = new MyRunnable();
                mHandler.postDelayed(mRunnable, 3000);
                break;

            case 1:
                openActivity(GuideActivity.class);
                finish();
                break;

            case 2:
                openActivity(RoleChoiceActivity.class);
                finish();
                break;

            case 3:
                openActivity(RoleLoginActivity.class);
                finish();
                break;

            case 4:
//                openActivity(MainActivity.class);
//                finish();
                break;

            default:
                break;
        }




    }

    private class MyRunnable implements Runnable {
        @Override
        public void run() {
            layout_index.setVisibility(View.GONE);
            layout_second.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.choose_true:
                PrefsUtil.setInt(StartFigureActivity.this, Constant.isFirstInstall,1);
                openActivity(GuideActivity.class);
                break;

            case R.id.choose_false:
                showDialog();
                break;

            default:
                break;
        }
    }


    /**
     * 弹出对话框
     */
    public void showDialog() {
        //实例化自定义对话框
        final ConstomDialog mdialog = new ConstomDialog(this);
        //对话框中退出按钮事件
        mdialog.setOnExitListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果对话框处于显示状态
                if (mdialog.isShowing()) {
                    //关闭对话框
                    mdialog.dismiss();

                }

            }
        });
        //对话框中取消按钮事件
        mdialog.setOnCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mdialog != null && mdialog.isShowing()) {
                    PrefsUtil.setInt(StartFigureActivity.this, Constant.isFirstInstall,0);
                    //关闭对话框
                    mdialog.dismiss();
                    //关闭当前界面
                    finish();
                }
            }
        });
        mdialog.show();

    }


}
